<template>
  <div class="box">
    <div class="con">
      <h3>超级管理系统</h3>
      <el-form
        :model="loginForm"
        status-icon
        :rules="rules"
        ref="domForm"
        class="demo-ruleForm"
        size="mini"
        @keyup.enter.native="submitForm('domForm')"
      >
        <el-form-item prop="account">
          <el-input
            type="text"
            v-model="loginForm.account"
            autocomplete="off"
            prefix-icon="iconfont icon-yonghu"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            :type="flag ? 'password' : 'text'"
            v-model="loginForm.password"
            autocomplete="off"
            prefix-icon="iconfont icon-mima"
          >
            <i
              slot="suffix"
              class="iconfont"
              :class="flag ? 'icon-eyes' : 'icon-eyes-'"
              @click="flag = !flag"
            ></i>
          </el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('domForm')"
            >提交</el-button
          >
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { validateAccount, validatePass } from '../../utils/validate.js'
import { checkLogin } from '../../api/info.js'
import local from '../../utils/local.js'
import { getSyncRouter } from '@/router/index.js'
export default {
  data() {
    return {
      loginForm: {
        account: '',
        password: ''
      },
      rules: {
        account: [{ validator: validateAccount, trigger: 'blur' }],
        password: [{ validator: validatePass, trigger: 'blur' }]
      },
      flag: true
    }
  },
  created() {},
  methods: {
    // 登录请求
    async login() {
      const data = await checkLogin({
        account: this.loginForm.account,
        password: this.loginForm.password
      })
      console.log(data)
      if (data.code === 0) {
        // 存token 跳转
        local.set('token', data.token)
        local.set('role', data.role)
        getSyncRouter()
        this.$router.push('/dashboard')
      } else {
        this.$message.error(data.msg)
      }
    },
    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          this.login()
        } else {
          return false
        }
      })
    }
  }
}
</script>

<style lang="less" scoped>
.box {
  height: 100%;
  background-color: #29394a;
  display: flex;
  justify-content: center;
  align-items: center;
  .con {
    width: 340px;
    height: 200px;
    text-align: center;
    color: #fff;
    /deep/.el-input__inner {
      color: #fff;
      background-color: rgba(0, 0, 0, 0);
    }
    /deep/.el-button {
      width: 100%;
    }
    h3 {
      font-weight: 700;
      letter-spacing: 3px;
      font-size: 20px;
      margin: 10px 0;
    }
  }
}
</style>
